/*
 * Copyright 2011 J. Auer, J. Andert, F.-J. Jendrusch, U. Celebi
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.example.android.notepad;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.example.android.notepad.NotePad.NoteColumns;

import de.fuberlin.replication.application.android.ManagedSynchronizableAbstract;
import de.fuberlin.replication.logging.android.AndroidLogger;
import de.fuberlin.replication.storage.android.sqlite.SQLiteStorage;

/**
 * This is the implementation of the synchronization interface. The superclass
 * <code>ManagedSynchronizableAbstract</code> takes care of handling communication with a communication manager.
 */
public class NotePadSynchronizable extends ManagedSynchronizableAbstract {
	private static final String TAG = "NotePadSynchronizable";

	private Context context;

	private AndroidLogger logger;

	private String address;

	private SQLiteStorage storage;

	/**
	 * Constructor. It takes an instance of <code>Context</code> as a paramter because it is needed by the database and
	 * activity.
	 * 
	 * @param context Android Context class
	 */
	public NotePadSynchronizable(Context context) {
		super();

		this.context = context;
	}

	@Override
	public AndroidLogger getLogger() {
		if (logger == null)
			logger = new AndroidLogger();

		return logger;
	}

	@Override
	public String getAddress() {
		if (address == null)
			address = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();

		return address;
	}

	@Override
	public SQLiteStorage getStorage() {
		if (storage == null)
			storage = new SQLiteStorage(context);

		return storage;
	}

	@Override
	public Note getObject(String name) {
		ContentResolver resolver = context.getContentResolver();

		Cursor cursor = resolver.query(NoteColumns.CONTENT_URI,
				new String[] {NoteColumns.TITLE, NoteColumns.NOTE},
				NoteColumns.TITLE + " = ?", new String[] {name}, null);

		if (!cursor.moveToFirst())
			return null;

		Note note = new Note();
		note.setTitle(cursor.getString(0));
		note.setNote(cursor.getString(1));
		note.setUpdate(false);

		cursor.close();

		return note;
	}

	@Override
	public void putObject(String name, Object o) {
		Note note = (Note) o;

		ContentValues values = new ContentValues();
		values.put(NoteColumns.TITLE, note.getTitle());
		values.put(NoteColumns.NOTE, note.getNote());
		values.put(Note.UPDATE, Boolean.valueOf(note.isUpdate()));

		ContentResolver resolver = context.getContentResolver();

		int count = resolver.update(NoteColumns.CONTENT_URI, values,
				NoteColumns.TITLE + " = ?", new String[] {note.getTitle()});

		if (count == 0)
			resolver.insert(NoteColumns.CONTENT_URI, values);
	}

	@Override
	public void onSynchronizationException(Exception e) {
		super.onSynchronizationException(e);

		Log.e(TAG, "Synchronization exception", e);
	}

	@Override
	public void onUpdateException(String name, Exception e) {
		super.onSynchronizationException(e);

		Log.e(TAG, "Update exception (name=" + name + ")", e);
	}
}